package com.yuan.rest.controller.es;


import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: SiYuan
 * @Date: 2019/2/21 10:35
 * @Description:
 */
public class es {

    public static void main(String[] args) {

      /*TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("123.207.174.215"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        Map<String, Object> json = new HashMap<String, Object>();
        json.put("user","kimchy");
        json.put("postDate","2013-01-30");
        json.put("message","trying out Elasticsearch");

        IndexResponse response = client.prepareIndex("fendo", "fendodate")
                .setSource(json)
                .get();
        System.out.println(response.getResult());
        System.out.println("--------------------");
        client.close();
        */

        Settings esSettings = Settings.builder()
                .put("cluster.name", "sy")
                .put("client.transport.sniff", false)
                .build();
        TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(esSettings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("123.207.174.215"), 9300));
            Map<String, Object> json = new HashMap<String, Object>();
            json.put("user","siyuan");
            json.put("s","2019-2-22");
            json.put("message","trying out Elasticsearch");
            IndexResponse response = client.prepareIndex("test2", "b")
                    .setSource(json)
                    .get();
            System.out.println(response.getResult());
            System.out.println("--------------------");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("ElasticsearchClient 连接成功");

    }
}
